let app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userphone:'',
    canCode: true,
    times: 60,
    code:'',
    r_code:'',
    pwd:'',
    r_pwd:'',
    mianmi: false
  },
  close(){
    this.setData({
      mianmi: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    var dh = wx.getStorageSync('userphone').replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    that.setData({
      userphone:wx.getStorageSync('userphone'),
      dh: dh
    })
    //is Density free
    wx.request({
      url: app.data.url + '/noPasswordPayStatus',
      method: "POST",
      header: app.getHeader(),
      success: function (res) {
        if(res.data.tips == 200){
          //on
          that.setData({
            checked: true
          })
        }else{
          //off
          that.setData({
            checked: false
          })
        }
      }
    })
    
  },
  getCode: function () {
    var that = this;
    wx.request({
      url: app.data.url + '/sendSms',
      method: "POST",
      header:app.getHeader(),
      data: {
        number: that.data.userphone,
        password: 'zhouleizhoulei'
      },
      success: function (res) {
        that.setData({
          r_code: res.data
        })
      }
    })
    that.setTimes()
  },
  setTimes: function () {
    var that = this;
    var time = that.data.times;
    time--;
    that.setData({
      times: time,
      canCode: false
    })
    var settimes = setTimeout(that.setTimes, 1000)
    if (time < 1) {
      clearTimeout(settimes)
      that.setData({
        canCode: true,
        times:60
      })
    }
  },
  code:function(e){
    var that =this;
    that.setData({
      code:e.detail.value
    })
  },
  pwd: function (e) {
    var that = this;
    var date = e.detail.value
   
    that.setData({
      pwd: date
    })
    console.log(that.data.pwd)
  },
  r_pwd: function (e) {
    var that = this;
    var date = e.detail.value
    that.setData({
      r_pwd: date
    })
  },
  changePass(){
    //修改密码
    const that = this
    if (!app.data.passSet){
      //未设置 提示设置
      wx.showModal({
        title: '提示',
        content: '请先充值，在充值后设置密码！',
        success(res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '../recharge/recharge',
            })
          }
        }
      })
    }else{
      that.setData({
        changePass: true
      })
    }
  },
  switchChange(e){
    let flag = 1
    const that = this
    if(e.detail.value){
      flag = 1
    }else{
      flag = 0
    }
    wx.request({
      url: app.data.url + '/updateNoPasswordPay',
      method: "POST",
      header: app.getHeader(),
      data: {
        noPasswordPay: flag
      },
      success: function (res) {
        if (res.data.tips == 200) {
          //on
          that.setData({
            mianmi: true
          })
        } else if (res.data.tips == 201){
          wx.showToast({
            title: '免密支付已关闭',
            icon: 'none',
            duration: 1800
          })
        } else {
          //off
          if (e.detail.value) {
            that.setData({
              checked: false
            })
          } else {
            that.setData({
              checked: true
            })
          }
          wx.showToast({
            title: '修改失败',
            icon: 'none',
            duration: 1800
          })
        }
      }
    })
  },
  closeTip() {
    this.setData({
      setSuccess: false,
      setErr: false
    })
  },
  submit:function(){
    var that =this;
    var pwd =that.data.pwd;
    var r_pwd =that.data.r_pwd;
    var code= that.data.code;
    if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(pwd)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确格式的密码！',
        showCancel: false
      })
      return false
    }
    if (pwd != r_pwd) {
      wx.showModal({
        title: '提示',
        content: '两次输入不一致！',
        showCancel: false
      })
      return false
    }
    if (that.data.code != that.data.r_code || that.data.code == '') {
      wx.showModal({
        title: '提示',
        content: '请输入正确的验证码！',
        showCancel: false
      })
      return false
    }
    console.log(pwd)
    console.log(r_pwd)
    wx.request({
      url: app.data.url +'/update_password',
      method:"POST",
      header:app.getHeader(),
      data:{
        write:that.data.code,
        rand:that.data.r_code,
        password:that.data.pwd,
        password_re:that.data.r_pwd
      },
      success:function(res){
        console.log(res)
        if(res.data==1){
          that.setData({
            changePass: false,
            setSuccess: true,
            code: '',
            r_code: '',
            pwd:'',
            r_pwd:'',
            canCode: false,
            times: 60
          })
        }else{
          this.setData({
            changePass: false,
            setErr: true
          })
        }
        setTimeout(function () {
          that.setData({
            setSuccess: false,
            setErr: false
          })
        }, 10000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})